package com.mop.app.controller.anymous;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mop.app.common.PageQueryWrapper;
import com.mop.app.common.enums.AppBizEnums;
import com.mop.app.common.exception.BizException;
import com.mop.app.dal.entity.AppSource;
import com.mop.app.dal.entity.AppSourceCategory;
import com.mop.app.dal.query.AppSourceQuery;
import com.mop.app.service.AppSourceCategoryService;
import com.mop.app.service.AppSourceService;
import com.mop.app.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 后台资源管理
 *
 * @author wchen
 * @since 2020-02-27
 */
@RestController
@RequestMapping("/anymous/admin35/")
public class AnyAdminController {

    @Autowired
    private FileService fileService;
    @Autowired
    private AppSourceService sourceService;
    @Autowired
    private AppSourceCategoryService sourceCategoryService;

    /**
     * 上传资源（同时图片）
     * @param request
     * @return
     */
    @RequestMapping(value = "source/saveWithPic")
    public void saveSource(HttpServletRequest request, @Param("categoryId") Long categoryId,
                           @Param("getTimes") Integer getTimes, @Param("description") String description,
                           @Param("downloadLink") String downloadLink, @Param("title") String title,
                           @Param("versionDesc") String versionDesc, @Param("versionNo") String versionNo) {
        AppSource appSource = new AppSource();
        appSource.setCategoryId(categoryId);
        appSource.setDescription(description);
        appSource.setDownloadLink(downloadLink);
        appSource.setGetTimes(getTimes);
        appSource.setTitle(title);
        appSource.setVersionDesc(versionDesc);
        appSource.setVersionNo(versionNo);

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        List<String> filePath = fileService.saveFile(multipartRequest);
        appSource.setPic(filePath.get(0));
        if (!sourceService.saveOrUpdate(appSource)) {
            throw new BizException(AppBizEnums.ERROR);
        }
    }

    /**
     * 保存或更新资源
     *
     * @param appSource
     * @return
     */
    @PostMapping("source/saveOrUpdate")
    public AppSource saveOrUpdateSource(@RequestBody AppSource appSource) throws Exception {
        if (sourceService.saveOrUpdate(appSource)) {
            return appSource;
        }
        throw new Exception();
    }


    /**
     * 资源列表分页查询
     *
     * @param query 默认使用实体作为查询参数，可根据业务需要替换为查询对象，如若替换为查询对象，请修改相应mapper文件的sql查询
     * @return 分页结果集
     */
    @PostMapping("source/list")
    public Page<AppSource> listSource(@RequestBody PageQueryWrapper<AppSourceQuery> query) {
        query.setQueryClazz(AppSourceQuery.class);
        Page pageList = sourceService.pageQuery(query);
        return pageList;
    }


    /**
     * 批量删除资源
     *
     * @param idList
     * @return
     */
    @PostMapping("source/remove")
    public void removeSource(@RequestBody List<Long> idList) throws Exception {
        if (sourceService.removeByIds(idList)) {
            return;
        }
        throw new Exception();
    }

    /**
     * 资源详情
     *
     * @param id
     * @return
     */
    @GetMapping("source/{id}")
    public AppSource getSourceById(@PathVariable Long id) {
        AppSource appSource = sourceService.getById(id);
        return appSource;
    }

    /****************************************  资源类别  ******************************************/


    /**
     * 保存或新增资源类别
     *
     * @param appSourceCategory
     * @return
     */
    @PostMapping("sourceCategory/saveOrUpdate")
    public AppSourceCategory saveOrUpdateSourceCategory(@RequestBody AppSourceCategory appSourceCategory) throws Exception {
        if (sourceCategoryService.saveOrUpdate(appSourceCategory)) {
            return appSourceCategory;
        }
        throw new Exception();
    }


    /**
     * 资源类别分页查询
     *
     * @param query 默认使用实体作为查询参数，可根据业务需要替换为查询对象，如若替换为查询对象，请修改相应mapper文件的sql查询
     * @return 分页结果集
     */
    @PostMapping("sourceCategory/list")
    public Page<AppSourceCategory> sourceCategoryList(@RequestBody PageQueryWrapper<AppSourceCategory> query) {

        query.setQueryClazz(AppSourceCategory.class);
        Page pageList = sourceCategoryService.pageQuery(query);
        return pageList;
    }


    /**
     * 批量删除资源类别
     *
     * @param idList
     * @return
     */
    @PostMapping("sourceCategory/remove")
    public void removeSourceCategory(@RequestBody List<Long> idList) throws Exception {
        if (sourceCategoryService.removeByIds(idList)) {
            return;
        }
        throw new Exception();
    }

    /**
     * 资源类别查询
     *
     * @param id
     * @return
     */
    @GetMapping("sourceCategory/{id}")
    public AppSourceCategory getSourceCategoryById(@PathVariable Long id) {
        AppSourceCategory appSourceCategory = sourceCategoryService.getById(id);
        return appSourceCategory;
    }


}
